package com.scorpio.baselibrary.ui;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;

import com.scorpio.baselibrary.PoorApplication;
import com.scorpio.baselibrary.utils.ToastUtil;

/**
 * created by zhud on 2018/10/25
 */
public abstract class BasicActivity extends AppCompatActivity {
    public static final int NORESULTCODE = -1;//普通无动作跳转码
    protected final String TAG = this.getClass().getSimpleName();
    protected Bundle mBundle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //防止重复创建的问题，第一次安装完成启动，点home键launcher icon启动会重复
        Intent intent = getIntent();
        if (!isTaskRoot() && intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(intent.getAction())) {
            finish();
            return;
        }
        mBundle = new Bundle();
    }

    //跳转
    protected void skipActivity(Class cls, int ResultCode, Intent intent, Bundle bundle) {
        intent.setClass(this, cls);
        if (bundle != null) intent.putExtras(bundle);
        if (NORESULTCODE != ResultCode) {
            startActivityForResult(intent, ResultCode);
        } else {
            startActivity(intent);
        }
        mBundle = null;
    }

    //跳转 带返回参数
    protected void skipActivity(Class cls, int ResultCode) {
        skipActivity(cls, ResultCode, new Intent(), mBundle);
    }

    //普通跳转
    protected void skipActivity(Class cls) {
        skipActivity(cls, NORESULTCODE, new Intent(), mBundle);
    }

    //跳转并且finish
    protected void skipActivityFinish(Class cls) {
        finish();
        skipActivity(cls);
    }

    //跳转 带返回参数 并且finish
    protected void skipActivityFinish(Class cls, int ResultCode) {
        finish();
        skipActivity(cls, ResultCode);
    }

    //finish 带返回码
    protected void finish(int result) {
        Intent intent = new Intent();
        if (mBundle != null) intent.putExtras(mBundle);
        setResult(result, intent);
        finish();
    }

    public void toast(@StringRes int string_id) {
        toast(getString(string_id));
    }

    public void toast(String text) {
        toast(text, ToastUtil.TOAST_SUCCEED);
    }

    public void toast(@StringRes int string_id, @ToastUtil.ToastType int toast_type) {
        toast(getString(string_id), toast_type);
    }


    public void toast(String text, @ToastUtil.ToastType int toast_type) {
        if (text != null) {
            PoorApplication.getInstance().showToast(this, text, toast_type);
        }
    }
}
